//
//  MyMessageViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/21.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "MyMessageViewController.h"
#import "CloverImgClick.h"

@interface MyMessageViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate,UIWebViewDelegate>
{
    UILabel *title;
    LeftTitleTextField *ReleasePeople;  //发布人
    LeftTitleTextField *ReleaseTime;    //发布日期
    UIView *under2;
    UIView *under3;
    UIView *under4;
    //    UIWebView *webViewzqx;
//    UIWebView *webView;
    UIImageView *messageImage;
    
    
    //---保存放大前原始图片在 window 上的坐标和大小
    CGRect originImageRect;
    //---利用 Scrollview 的缩放功能来缩放图像
    UIScrollView *ZQXscrollView;
    //---用于按屏幕比例缩放图像
    UIView* container_View;
    //---保存缩放的图像
    UIImageView *imageView;
    
    CGSize ZQXimageSize;
    
    UIWindow *NewWindow;
    UIView *mask;
    UIButton *readAll;
    
    
}

@end

@implementation MyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资讯详情";
    self.view.backgroundColor = [UIColor blackColor];
    [self setUI];
    [self getData];

}

- (void)setUI{
    WS(ws);
    self.view.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    scrollView.bounces = NO;
    scrollView.scrollEnabled = YES;
    scrollView.scrollsToTop = NO;
    UIView *container = [UIView new];
    [scrollView addSubview:container];
    container.backgroundColor = [UIColor colorWithWhite:0.911 alpha:1.000];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scrollView);
        make.edges.equalTo(scrollView).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    CGFloat height  = 30*FitSize;
    CGFloat width = 250*FitSize;
    //标题
    title = [[UILabel alloc]init];
    title.font = [UIFont systemFontOfSize:18];
    title.text = self.dic[@"nTitlte"];
    title.numberOfLines = 0;
    [title sizeToFit];
    
    
    title.textAlignment = NSTextAlignmentCenter;
    [container addSubview:title];
    
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        //        make.size.mas_equalTo(textSize);
        
    }];
    
    
    UIView *under = [[UIView alloc]init];
    under.backgroundColor = [UIColor whiteColor];
    [container addSubview:under];
    [under mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(title.mas_bottom).offset(25);
    }];
    under.layer.cornerRadius = 10;
    
    UIColor *fontColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.013 alpha:1.000];
    ReleasePeople = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"发布人:"];
    [ReleasePeople hideUnderLine];
    [under addSubview:ReleasePeople];
    [ReleasePeople mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(25);
        make.height.mas_equalTo(height);
    }];
    
    ReleaseTime = [[LeftTitleTextField alloc]initWithUnderLineColor:fontColor height:height width:width title:@"发日期:"];
    [ReleaseTime hideUnderLine];
    [under addSubview:ReleaseTime];
    [ReleaseTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ReleasePeople.mas_bottom).offset(5);
        make.left.equalTo(ReleasePeople);
        make.height.mas_equalTo(height);
    }];
    [under mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ReleaseTime.mas_bottom).offset(15);
    }];
    
    
    
    under2 = [[UIView alloc]init];
    [container addSubview:under2];
    [under2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under.mas_bottom).offset(30);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(30);
    }];
    under2.layer.cornerRadius = 10;
    under2.backgroundColor = [UIColor whiteColor];
    
//    webView = [UIWebView new];
//    [under2 addSubview:webView];
//    webView.delegate = self;
//    webView.scalesPageToFit = YES;
//    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(under2).width.insets(UIEdgeInsetsMake(0, 0, 0, 0));
//    }];
    
    
    messageImage = [[UIImageView alloc]init];
    [under2 addSubview:messageImage];
    messageImage.userInteractionEnabled = YES;
    [messageImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(under2).with.insets(UIEdgeInsetsMake(0,0,0,0));   //上左右下
    }];
    
    readAll = [UIButton buttonWithType:UIButtonTypeCustom];
//    readAll.backgroundColor = [UIColor redColor];
    [readAll addTarget:self action:@selector(readAllTap) forControlEvents:UIControlEventTouchUpInside];
    [readAll setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [readAll setTitle:@"查看正文" forState:UIControlStateNormal];
    [under2 addSubview:readAll];
    [readAll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.centerY.equalTo(under2);
        make.width.equalTo(under2);
    }];
    readAll.hidden = YES;
    
    under3 = [[UIView alloc]init];
    [container addSubview:under3];
    [under3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under2.mas_bottom).offset(20);
        make.left.equalTo(under);
        make.right.equalTo(under);
        make.height.mas_equalTo(40);
    }];
    under3.backgroundColor = [UIColor whiteColor];
    under3.layer.cornerRadius = 10;
    
   
    
    
    
    UILabel *attachment = [[UILabel alloc]init];
    [under3 addSubview:attachment];
    [attachment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(height);
        make.centerY.equalTo(under3);
    }];
    attachment.text = @"相关附件:";
    
  
    [container mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(under3.mas_bottom).offset(30);
    }];
    
    
    
}

- (void)readAllTap{
    readAll.hidden = YES;
    NSString *urlString = [NSString stringWithFormat:@"%@%@",IPAddress,self.dic[@"FilePath"]];
    [messageImage sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"me"] options:SDWebImageLowPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (!error) {
            [self config:image.size];
        }else{
            [XHToast showTopWithText:@"服务器资源不存在!"];
        }
        
    }];

}

- (void)setValue{
    readAll.hidden = NO;
//    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.dic[@"nContent"]]]];
    ReleasePeople.text = self.dic[@"nCreateRealName"];
    ReleaseTime.text = self.dic[@"nCreateTime"];
    
    
}
//配置图片大小
- (void)config:(CGSize)imageSize{
    ZQXimageSize = imageSize;
    float fitHeight = 0;
    fitHeight = imageSize.height*(messageImage.frame.size.width/imageSize.width);
    NSLog(@"图片高:%f",fitHeight);
    [under2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(fitHeight);
    }];
    UITapGestureRecognizer *tap_Gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];
    [tap_Gesture setNumberOfTapsRequired:1];
    messageImage.userInteractionEnabled = YES;
    [messageImage addGestureRecognizer:tap_Gesture];
}

//点击图片
- (void)tapImage:(UITapGestureRecognizer *)sender{
    [self newWindow];
}

- (void)newWindow{
    
    NewWindow = [UIApplication sharedApplication].keyWindow;
    originImageRect = [messageImage convertRect:messageImage.bounds toView:NewWindow];
    [self.navigationController setNavigationBarHidden:YES];
    mask = [[UIView alloc]initWithFrame:self.view.bounds];
    [NewWindow addSubview:mask];
    mask.backgroundColor = [UIColor whiteColor];
    
    
    ZQXscrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 18, KSWidth, KSHeight)];
    ZQXscrollView.backgroundColor = [UIColor clearColor];
    ZQXscrollView.scrollsToTop = NO;
    ZQXscrollView.bounces = NO;
    //    ZQXscrollView.bouncesZoom = NO;
    ZQXscrollView.maximumZoomScale=5.0;//图片的放大倍数
    ZQXscrollView.minimumZoomScale=1.0;//图片的最小倍率
    
    ZQXscrollView.delegate=self;
    
    imageView=[[UIImageView alloc]init];
    //计算出
    ZQXscrollView.contentSize=CGSizeMake(KSWidth, (KSWidth/ZQXimageSize.width)*ZQXimageSize.height);
    imageView.frame = originImageRect;
    imageView.image = messageImage.image;
    [ZQXscrollView addSubview:imageView];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [mask addSubview:ZQXscrollView];
    imageView.userInteractionEnabled=YES;//注意:imageView默认是不可以交互,在这里设置为可以交互
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    tap.numberOfTapsRequired=1;//单击
    tap.numberOfTouchesRequired=1;//单点触碰
    [imageView addGestureRecognizer:tap];
    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap)];
    doubleTap.numberOfTapsRequired=2;//避免单击与双击冲突
    [tap requireGestureRecognizerToFail:doubleTap];
    [imageView addGestureRecognizer:doubleTap];
    
    //---动画显示
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        imageView.bounds = CGRectMake(0, 0, KSWidth, (KSWidth/ZQXimageSize.width)*ZQXimageSize.height);
        imageView.center = mask.center;
        
        ZQXscrollView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
        
    }
                     completion:^(BOOL finished)
     {
         //---隐藏状态栏
//         [UIApplication.sharedApplication setStatusBarHidden:true withAnimation:UIStatusBarAnimationSlide];
     }];
    
}


//关闭图片浏览器
- (void)hideImage:(UITapGestureRecognizer *)sender{
    ZQXscrollView.backgroundColor=[UIColor clearColor];
    mask.backgroundColor = [UIColor clearColor];
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //---缩放到原始大小时，必须同时缩放到原始的倍率，否则放大的情况位置偏移
        ZQXscrollView.zoomScale = 1.0f;
        imageView.frame = originImageRect;
    } completion:^(BOOL finished) {
        [ZQXscrollView removeFromSuperview];
        [mask removeFromSuperview];
        [imageView removeFromSuperview];
        
    }];
    [self.navigationController setNavigationBarHidden:NO];
    
    
}

//双击还原原本大小
-(void)doubleTap{
    ZQXscrollView.zoomScale = 1.0f;
    
    
}




- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{
    scrollView.contentSize = imageView.frame.size;
}


#pragma mark - UIScrollView Delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    //---缩放容器container_View
    return imageView;
}



- (void)getData{
    NSString *ashx = @"/Server/NewsHandlerNew.ashx";
    
    NSDictionary *dic = @{@"act":@"getd",@"nid":self.dic[@"ID"],@"uname":[UserInfo getUserName],@"upwd":[UserInfo getPassWord]};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSLog(@"公告通知详情:%@",model);
                NSArray *arrayTemp = model[@"data"];
                if (arrayTemp.count > 0) {
                    self.dic = model[@"data"][0];
                }
                [self setValue];
                
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
                
            }
        }else{
            NSLog(@"错误%@",error);
            
            [XHToast showBottomWithText:@"请求失败"];
        }
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [LBProgressHUD hideAllHUDsForView:webView animated:YES];
//    [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom=1.6"];
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '190%'"];
    CGSize actualSize = [webView sizeThatFits:CGSizeZero];
    CGRect newFrame = webView.frame;
    newFrame.size.height = actualSize.height*1.6;
    NSLog(@"浏览器高度:%f",newFrame.size.height);
    webView.frame = newFrame;
    
    NSLog(@"浏览器尺寸：%@",NSStringFromCGRect(newFrame));
    [under2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(webView.frame.size.height+30);
    }];
    [under3 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(under2.mas_bottom).offset(20);
    }];
    
}





@end
